﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Happy.PessimisticLock.Test
{
    [TestClass]
    public class LockManagerTest
    {
        [TestMethod]
        public void LockManager_Test()
        {
            var entity = "Test";
            var key = "001";
            var owner1 = "un-known1";
            var owner2 = "un-known2";

            var lockManager = new MemoryLockManager();

            Assert.IsTrue(lockManager.AcquireLock(entity, key, owner1, new DateTimeExpirationPolicy(5)));
            Assert.IsFalse(lockManager.AcquireLock(entity, key, owner2, new DateTimeExpirationPolicy(5)));

            lockManager.ReleaseLock(entity, key, owner1);

            Assert.IsTrue(lockManager.AcquireLock(entity, key, owner2, new DateTimeExpirationPolicy(5)));
            lockManager.ReleaseLock(entity, key, owner2);
        }
    }
}
